from effect.util.console.SuperConsole import *


class select:
    def __init__(self, title, options, tips=None, print_mode=None):
        if tips is not None:
            self.tips = tips
            if len(options) != len(tips):
                raise AttributeError("错误数据")
        else:
            self.tips = ["" for i in range(len(options))]
        self.print_mode = print_mode
        self.title = title
        self.options = options
        self.actives = 0

    def show(self,color=None):
        cls()
        print(self.title)
        print("\n"*4)
        for index, value in enumerate(self.options):
            if index == self.actives:
                wLine( val=value,cor='black' ,bg='white')
                if self.print_mode == "call":
                    self.tips[self.actives]()
                else:
                    if color == "ignore":
                        panel(val=f'{self.tips[self.actives]}')
                    else:
                        panel(val=f'{self.tips[self.actives]}', cor='green')

            else:
                print(value)
        return self.movepoint(readKey())

    def movepoint(self, key=None, direction=None):
        if direction is not None:
            if direction == 'up' and self.actives > 0:
                self.actives -= 1
            elif direction == 'up' and self.actives == 0:
                self.actives = len(self.options) - 1
            elif direction == "down" and self.actives == len(self.options) - 1:
                self.actives = 0
            elif direction == 'down' and self.actives < len(self.options) - 1:
                self.actives += 1
            return "??"
        elif key is not None:
            if key == 'UpArrow':
                self.movepoint(direction='up')
            elif key == "DownArrow":
                self.movepoint(direction='down')
            elif key == 'Enter':
                return

        self.show()

    def getvalue(self):
        return self.options[self.actives]

    def getindex(self):
        return self.actives



